<?php

class CouponModel extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * 新增||修改优惠券
     * @param type $data
     */
    public function initCoupon($data) {
        if ($data['id'] > 0) {
            $this->db->where('id', $data['id']);
            $ret = $this->db->update('wechat_coupon', $data);
        } else {
            $ret = $this->db->insert('wechat_coupon', $data);
        }
        return $ret;
    }

    /**
     * 根据公众号ID列出所属
     * 优惠券列表
     * (在控制台中使用)
     * @param type $public_id
     * @return type
     */
    public function getCouponListById($public_id) {
        $sql = " SELECT c.id as coupon_id , s.shop_name , c.coupon_start , c.coupon_end , c.coupon_info , c.coupon_count , c.coupon_use , c.coupon_receive "
                . " FROM wechat_coupon c,wechat_public p,wechat_shop s "
                . " WHERE (c.public_id = p.id) AND "
                . " (c.shop_id = s.id) AND "
                . " (p.id = $public_id) ";
        $data = $this->db->query($sql);
        return $data->result_array();
    }

    /**
     * 根据优惠券ID
     * 获取优惠券详细信息
     * (在wechat中使用)
     * @param type $coupon_id
     * @return type
     */
    public function getCouponDetailById($coupon_id) {

        // 查询优惠券信息
        $coupon_query = $this->db->get_where('wechat_coupon', array('id' => $coupon_id));
        $coupon = $coupon_query->row_array();
        $data['coupon_id'] = $coupon['id'];
        $data['coupon_info'] = $coupon['coupon_info'];
        $data['coupon_start'] = $coupon['coupon_start'];
        $data['coupon_end'] = $coupon['coupon_end'];
        $data['coupon_rule'] = $coupon['coupon_rule'];
        $data['coupon_count'] = $coupon['coupon_count'];
        $data['branche_id'] = $coupon['branche_id'];

        // 查询公众号详情
        $public_query = $this->db->get_where('wechat_public', array('id' => $coupon['public_id']));
        $public = $public_query->row_array();

        $data['public_desc'] = $public['desc'];
        $data['public_qrcode'] = $public['qrcode'];
        $data['public_id'] = $public['id'];

        // 查询门店信息
        if ($coupon['branche_id'] == 0) {
            $shop_query = $this->db->get_where('wechat_shop', array('id' => $coupon['shop_id']));
            $shop = $shop_query->row_array();
            $data['shop_id'] = $shop['id'];
            $data['shop_img_0'] = $shop['shop_img_0'];
            $data['shop_img_1'] = $shop['shop_img_1'];
            $data['shop_img_2'] = $shop['shop_img_2'];
            $data['shop_img_3'] = $shop['shop_img_3'];
            $data['shop_name'] = $shop['shop_name'];
            $data['shop_province'] = $shop['shop_province'];
            $data['shop_city'] = $shop['shop_city'];
            $data['shop_address'] = $shop['shop_address'];
            $data['shop_phone'] = $shop['shop_phone'];
            $data['shop_desc'] = $shop['shop_desc'];
        } else {
            $sql = " SELECT s.id as shop_id , s.shop_name , s.shop_desc , s.shop_img_0 , s.shop_img_1 , s.shop_img_2 , s.shop_img_3 , "
                    . " b.branche_name , b.branche_province , b.branche_city , b.branche_address , b.branche_phone "
                    . " FROM wechat_shop s , wechat_shop_branche b "
                    . " WHERE (b.shop_id = s.id) AND "
                    . " (b.id = " . $coupon['branche_id'] . ")";
            $shop_query = $this->db->query($sql);
            $shop = $shop_query->row_array();
            $data['shop_id'] = $shop['shop_id'];
            $data['shop_img_0'] = $shop['shop_img_0'];
            $data['shop_img_1'] = $shop['shop_img_1'];
            $data['shop_img_2'] = $shop['shop_img_2'];
            $data['shop_img_3'] = $shop['shop_img_3'];
            $data['shop_name'] = $shop['shop_name'];
            $data['shop_province'] = $shop['branche_province'];
            $data['shop_city'] = $shop['branche_city'];
            $data['shop_address'] = $shop['branche_address'];
            $data['shop_phone'] = $shop['branche_phone'];
            $data['shop_desc'] = $shop['shop_desc'];
        }

        return $data;
    }

    /**
     * 优惠券列表
     */
    public function getCouponList() {
        $sql = " SELECT s.shop_name , c.id , c.coupon_info , c.coupon_count , c.coupon_start , c.coupon_end FROM "
                . " wechat_shop s , wechat_coupon c "
                . " WHERE (s.id = c.shop_id) "
                . " AND (s.shop_status = 1)";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

}
